package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/4/15 10:59
 */
public class C896 {

    // https://leetcode-cn.com/problems/monotonic-array/
    // 两次遍历
    public static boolean isMonotonic(int[] A) {
        if(A.length == 1) {
            return true;
        }
        boolean flag = A[0] <= A[1];     // 从小到大
        int i;
        for (i = 0; i < A.length - 1; i++) {
            if(A[i] > A[i+1]) {
                break;
            }
        }
        if (i == A.length - 1) return true;
        for (i = 0; i < A.length - 1; i++) {
            if(A[i] < A[i+1]) {
                break;
            }
        }
        return i == A.length - 1;
    }
    // 一次遍历
    public static boolean isMonotonic2(int[] A) {
        boolean inc = true, dec = true;
        for (int i = 0; i < A.length - 1; i++) {
            if(A[i] > A[i + 1]) {
                inc = false;
            }else if(A[i] < A[i+1]) {
                dec = false;
            }
            if(!inc && !dec) return false;
        }
        return true;
    }

    // for test
    public static void main(String[] args) {
//        int[] A = {1,2,4,5};
//        int[] A = {1,2,2,3};
//        int[] A = {6,5,4,4};
        int[] A = {1,3,2};
        System.out.println(isMonotonic(A));
        System.out.println(isMonotonic2(A));
    }

}
